package com.ldn.server.mqtt.listener;


import com.ldn.server.mqtt.covert.MqttCovert;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.dromara.mica.mqtt.core.server.event.IMqttSessionListener;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.tio.core.ChannelContext;


/**
 *  mqtt SessionListener 监听器
 *
 * @author 李大牛家的牛 (scafel@163.com)
 * @product serverless
 * @className SessionListener
 * @createDate 2025/3/4 - 13:15
 * @description mqtt SessionListener 监听器
 **/
@Slf4j
@Service
public class SessionListener implements IMqttSessionListener {

    @Resource
    private ApplicationContext applicationContext;

    @Override
    public void onSubscribed(ChannelContext context, String clientId, String topicFilter, MqttQoS mqttQoS) {
        log.info("onSubscribed clientId:{},topicFilter:{},mqttQoS:{}", clientId, topicFilter, mqttQoS);
        MqttCovert covert = applicationContext.getBean(MqttCovert.class);
        covert.onSubscribed(context, clientId, topicFilter, mqttQoS.value());
    }

    @Override
    public void onUnsubscribed(ChannelContext context, String clientId, String topicFilter) {
        log.info("onUnsubscribed clientId:{},topicFilter:{}", clientId, topicFilter);
        MqttCovert covert = applicationContext.getBean(MqttCovert.class);
        covert.onUnsubscribed(context, clientId, topicFilter);
    }
}
